function hfile = aft_ReloadFromDisk(hfile)
% AFT::ReloadFromDisk  - try reloading the object from disk
%
% FORMAT:       obj.ReloadFromDisk;
%
% No input / output fields.
%
% TYPES: ALL
%
% Note: if the object requires large amount of memory, this method
%       is bound to fail!

% Version:  v0.8a
% Build:    9102122
% Date:     Oct-21 2009, 10:12 PM CEST
% Author:   Jochen Weber, SCAN Unit, Columbia University, NYC, NY, USA
% URL/Info: http://wiki.brainvoyager.com/BVQXtools

% global config
global bvqxfile_config;

% only valid for single file
if nargin ~= 1 || ...
    numel(hfile) ~= 1 || ...
   ~isBVQXfile(hfile, true)
    error( ...
        'BVQXfile:BadArgument', ...
        'Invalid call to %s.', ...
        mfilename ...
    );
end

% get object's super-struct
sc = bvqxfile_getscont(hfile.L);
if isempty(sc.F) || ...
   ~isabsolute(sc.F) || ...
    exist(sc.F, 'file') ~= 2
    error( ...
        'BVQXfile:BadFileName', ...
        'Cannot reload from disk, invalid file: ''%s''.', ...
        sc.F ...
    );
end

% try to load file
rs = bvqxfile_config.reloadsame;
try
    bvqxfile_config.reloadsame = true;
    nobj = BVQXfile(sc.F);
    if ~isBVQXfile(nobj, sc.S.Extensions{1})
        error('LOAD_ERROR');
    end
    bvqxfile_config.reloadsame = rs;
catch
    bvqxfile_config.reloadsame = rs;
    error( ...
        'BVQXfile:InternalError', ...
        'Error re-loading object from file: ''%s''.', ...
        sc.F ...
    );
end

% copy contents from reloaded object to old object
bvqxfile_setcont(hfile.L, bvqxfile_getcont(nobj.L));
bvqxfile_clear(nobj.L);
